'use strict';

const Controller = require('egg').Controller;
const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'equipment'
});

class equipmentController extends Controller {
    //查询所有仪器信息的方法
    //执行数据库操作并执行回调，有err则抛出异常，控制台输出结果
    async  findEquipmentAll() {
        connection.query('select * from e_instrument', function (error, results, fields) {
            if (error) {
                console.log('出错了！');
            }
            console.log('查询结果为: ', results);
        });
        connection.end();
    }

    async updateEquipmentById() {
        console.log('EquipmentService======>updateById()');
        data = connection.query("UPDATE e_instrument SET NAME='LED中控制屏',manufactor='纳米软件' WHERE id =6");
        console.log(data);
        connection.end();
    }

    //新增仪器
    async insertEquipment() {
        console.log('EquipmentService======>insertEquip()');
        data = connection.query("INSERT INTO e_instrument(NAME,TYPE,manufactor,model) VALUES('测汞仪',3,'安泰','实验室仪器')");
        console.log(data);
        connection.end();
    }
    //根据id删除仪器
    async deleteEquipmentById() {
        console.log('EquipmentService======>deleteById()');
        data = connection.query('DELETE FROM e_instrument WHERE id=11');
        console.log(data);
        connection.end();
    }
}

module.exports = equipmentController;